package framework;

import java.util.HashSet;
import java.util.Set;

/**
 * Implements common game functionality
 *
 * @author Denis DIR Rozhnev
 */
public abstract class AbstractGame implements Game {
    protected int startScore;
    protected Business biz;
    protected Person[] peers;
    private int[] scores;
    protected int profit;
    protected int turnNumber;
    private Set<Listener> listeners = new HashSet<Listener>();

    @Override
    public void init(Person[] persons, Business business) {
        this.biz = business;
        this.peers = persons;
        scores = new int[peers.length];
        profit = 0;
        if (startScore != 0) {
            for (int i = 0; i < scores.length; i++) {
                scores[i] = startScore;
            }
        }

    }

    @Override
    public Person getPerson(int i) {
        return peers[i];
    }

    @Override
    public int getProfit() {
        return profit;
    }

    @Override
    public int[] getScores() {
        return scores;
    }

    protected void addScore(int personNumber, int score) {
        scores[personNumber] += score;
        profit += score;
    }

    @Override
    public int getScore(Person person) {
        for (int i = 0; i < peers.length; i++) {
            if (person == peers[i])
                return scores[i];
        }
        return -1;
    }

    @Override
    public Business getBiz() {
        return biz;
    }

    public int getTurnNumber() {
        return turnNumber;
    }

    @Override
    public void addListener(Listener listener) {
        listeners.add(listener);
    }

    protected void notifyListeners(int turnCount) {
        for (Listener listener : listeners) {
            listener.tuned(turnCount);
        }
/*      todo
        renderer.updateScores(); //done
        repaintComponent.repaint();
*/
    }
}
